package com.langyiquan.service.impl;

import com.langyiquan.bo.UserBO;
import com.langyiquan.bo.UserInfoBO;
import com.langyiquan.enums.Sex;
import com.langyiquan.mapper.UsersMapper;
import com.langyiquan.pojo.Users;
import com.langyiquan.service.UserService;
import com.langyiquan.utils.DateUtil;
import com.langyiquan.utils.Md5Util;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    private static final String USER_FACE = "https://uufe-web.oss-cn-beijing.aliyuncs.com/PicLib/uunote/userphoto/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20190926150616_1569481588771.jpg";
    @Autowired
    public Sid sid;

    @Autowired
    public UsersMapper usersMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {

        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        Users user = usersMapper.selectOneByExample(example);
        return user == null ? false : true;

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateUserInfo(String userId, UserInfoBO userInfoBO) {
        Users updateUser = new Users();
        BeanUtils.copyProperties(userInfoBO,updateUser);
        updateUser.setId(userId);
        updateUser.setUpdatedTime(new Date());
        usersMapper.updateByPrimaryKeySelective(updateUser);
        return queryUserInfo(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateUserFace(String userId, String faceUrl) {
        Users updateUser = new Users();
        updateUser.setId(userId);
        updateUser.setFace(faceUrl);
        usersMapper.updateByPrimaryKeySelective(updateUser);
        return queryUserInfo(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users createWechatUser(Users users) {
        Users user = new Users();
        String userId = sid.nextShort();
        user.setId(userId);
        user.setNickname(users.getNickname());
        user.setFace(users.getFace());
        user.setSex(users.getSex());
        user.setOpenid(users.getOpenid());
        user.setUnionid(users.getUnionid());
        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());
        usersMapper.insert(user);
        return user;

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateWechatUser(String userId, Users users) {
        Users updateUser = new Users();
        updateUser.setId(userId);
        updateUser.setNickname(users.getNickname());
        updateUser.setFace(users.getFace());
        updateUser.setUpdatedTime(new Date());
        usersMapper.updateByPrimaryKeySelective(updateUser);
        return queryUserInfo(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users getUserByOpenid(String openid) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("openid",openid);
        return usersMapper.selectOneByExample(example);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users createUser(UserBO userBO) {

        Users user = new Users();
        String userId = sid.nextShort();
        user.setId(userId);
        user.setUsername(userBO.getUsername());
        try {
            user.setPassword(Md5Util.getMd5Str(userBO.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        user.setNickname(userBO.getUsername());
        user.setFace(USER_FACE);
        user.setBirthday(DateUtil.stringToDate("1990-01-01"));
        user.setSex(Sex.SECRET.type);
        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());
        usersMapper.insert(user);
        return user;

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserInfo(String userId) {
        Users user = usersMapper.selectByPrimaryKey(userId);
        user.setPassword(null);
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String password) {

        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        try {
            criteria.andEqualTo("password",Md5Util.getMd5Str(password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        Users user = usersMapper.selectOneByExample(example);
        return user;
    }
}
